package com.zxy.javaarithmetic.chapter_2_LinkList;

/*
 *  @项目名：  study
 *  @包名：    com.zxy.javaarithmetic.chapter_2_LinkList
 *  @文件名:   P_1_PrintCommon
 *  @创建者:   zhangxy
 *  @创建时间:  2018/12/6 10:24
 *  @描述：    打印出两个有序（升序）链表的公共部分
 */
public class P_1_PrintCommon {

	public static void main(String[] args){
       int[] data1 = new int[]{2,3,5,6,7,8};
       int[] data2 = new int[]{1,3,6,8,9};

       Node head1 = new Node(data1[0]);
       Node temp = head1;
		for (int i = 1; i < data1.length; i++) {
			temp.next = new Node(data1[i]);
			temp = temp.next;
		}

		Node head2 = new Node(data2[0]);
		temp = head2;
		for (int i = 1; i < data2.length; i++) {
			temp.next = new Node(data2[i]);
			temp = temp.next;
		}
		printCommon(head1,head2);
	}

	private static void printCommon(Node head1,Node head2){
        if(head1 == null || head2 == null){
        	System.out.println("没有公共数据");
        	return;
        }
        Node node1 = head1;
        Node node2 = head2;
        while (node1 != null && node2 != null){
        	if(node1.value > node2.value){
        		node2 = node2.next;
	        }else if(node1.value < node2.value){
		        node1 = node1.next;
	        }else {
        		System.out.print(node1.value+"  ");
        		node1 = node1.next;
        		node2 = node2.next;
	        }
        }
	}

}
